<?php
session_cache_expire(999999999);
if (!isset($_SESSION)) {
    session_start();
}
include "../../operations/connection_ajax.php";
if (!isset($_GET['pages'])) {
    $page = 1;
} else {
    $page = $_GET['pages'];
}

if (isset($_REQUEST['searchAll'])) {
    //$_SESSION['searchstring'] = '';
    unset($_SESSION['searchstring']);
}

if (isset($_SESSION['searchstring'])) {
    $temp_s = $_SESSION['searchstring'];
} else {
    $temp_s = '';
}

$rows_per_page = 30;
$page_start = ( $page - 1 ) * $rows_per_page;
$page_end = $page * $rows_per_page;

//delete
function getlang() {
    if (isset($_SESSION['lang']))
        $lang = $_SESSION['lang'];
    if (empty($lang) || $lang == "EN") {
        $value = "../../value/value_en.php";
    } else if ($lang == "VN") {
        $value = "../../value/value_vn.php";
    } else {
        $value = "../../value/value_kr.php";
    }
    return $value;
}

$value = getlang();
include($value);
$idemployee = $_REQUEST['idemployee'];
$sqldelete = "delete from employee where IDEmployee = '$idemployee'";
mysql_query($sqldelete);
$sqldelete = "delete from sa_regulation_salary where employeeID = '$idemployee'";
mysql_query($sqldelete);
$str = "";
if ($_SESSION['db'] == 'elitech') {
    $str = "AND e.Type_employee=1 ";
}
$sqlempworking = mysql_query("SELECT e.IDEmployee,e.FullName,DATE_FORMAT(e.Birthday,'%d/%m/%Y') as Birthday,e.UserName,al.SexName,dl.DepartmentName,e.Position,e.StartDate,e.Address,e.CMND,e.Mobile   
							FROM employee e ,department_lang dl, ac_sex_lang al, roles rl 
						   where e.IDDepartment = dl.IDDepartment and dl.LangID = '" . $_SESSION['lang'] . "' 
						   and e.Sex = al.SexID and al.LangID = '" . $_SESSION['lang'] . "'  
						   and e.IDRole= rl.IDRole and e.IDRole != 1 and e.State = 1 $str" . $temp_s );

$number_of_page = ceil(mysql_num_rows($sqlempworking) / $rows_per_page);

function ViPAencode($text) {
    $origin = array(
        '/([0-9])/',
        '/a/', '/à/', '/ả/', '/ã/', '/á/', '/ạ/',
        '/ă/', '/ằ/', '/ẳ/', '/ẵ/', '/ắ/', '/ặ/',
        '/â/', '/ầ/', '/ẩ/', '/ẫ/', '/ấ/', '/ậ/',
        '/e/', '/è/', '/ẻ/', '/ẽ/', '/é/', '/ẹ/',
        '/ê/', '/ề/', '/ể/', '/ễ/', '/ế/', '/ệ/',
        '/i/', '/ì/', '/ỉ/', '/ĩ/', '/í/', '/ị/',
        '/o/', '/ò/', '/ỏ/', '/õ/', '/ó/', '/ọ/',
        '/ô/', '/ồ/', '/ổ/', '/ỗ/', '/ố/', '/ộ/',
        '/ơ/', '/ờ/', '/ở/', '/ỡ/', '/ớ/', '/ợ/',
        '/u/', '/ù/', '/ủ/', '/ũ/', '/ú/', '/ụ/',
        '/ư/', '/ừ/', '/ử/', '/ữ/', '/ứ/', '/ự/',
        '/y/', '/ỳ/', '/ỷ/', '/ỹ/', '/ý/', '/ỵ/',
        '/A/', '/À/', '/Ả/', '/Ã/', '/Á/', '/Ạ/',
        '/Ă/', '/Ằ/', '/Ẳ/', '/Ẵ/', '/Ắ/', '/Ặ/',
        '/Â/', '/Ầ/', '/Ẩ/', '/Ẫ/', '/Ấ/', '/Ậ/',
        '/E/', '/È/', '/Ẻ/', '/Ẽ/', '/É/', '/Ẹ/',
        '/Ê/', '/Ề/', '/Ể/', '/Ễ/', '/Ế/', '/Ệ/',
        '/I/', '/Ì/', '/Ỉ/', '/Ĩ/', '/Í/', '/Ị/',
        '/O/', '/Ò/', '/Ỏ/', '/Õ/', '/Ó/', '/Ọ/',
        '/Ô/', '/Ồ/', '/Ổ/', '/Ỗ/', '/Ố/', '/Ộ/',
        '/Ơ/', '/Ờ/', '/Ở/', '/Ỡ/', '/Ớ/', '/Ợ/',
        '/U/', '/Ù/', '/Ủ/', '/Ũ/', '/Ú/', '/Ụ/',
        '/Ư/', '/Ừ/', '/Ử/', '/Ữ/', '/Ứ/', '/Ự/',
        '/Y/', '/Ỳ/', '/Ỷ/', '/Ỹ/', '/Ý/', '/Ỵ/',
        '/d/', '/đ/', '/D/', '/Đ/');

    $codes = array(
        "-$1",
        'a0', 'a02', 'a03', 'a04', 'a05', 'a06',
        'a1', 'a12', 'a13', 'a14', 'a15', 'a16',
        'a2', 'a22', 'a23', 'a24', 'a25', 'a26',
        'e0', 'e02', 'e03', 'e04', 'e05', 'e06',
        'e1', 'e12', 'e13', 'e14', 'e15', 'e16',
        'i0', 'i02', 'i03', 'i04', 'i05', 'i06',
        'o0', 'o02', 'o03', 'o04', 'o05', 'o06',
        'o1', 'o12', 'o13', 'o14', 'o15', 'o16',
        'o2', 'o22', 'o23', 'o24', 'o25', 'o26',
        'u0', 'u02', 'u03', 'u04', 'u05', 'u06',
        'u1', 'u12', 'u13', 'u14', 'u15', 'u16',
        'y0', 'y02', 'y03', 'y04', 'y05', 'y06',
        'A0', 'A02', 'A03', 'A04', 'A05', 'A06',
        'A1', 'A12', 'A13', 'A14', 'A15', 'A16',
        'A2', 'A22', 'A23', 'A24', 'A25', 'A26',
        'E0', 'E02', 'E03', 'E04', 'E05', 'E06',
        'E1', 'E12', 'E13', 'E14', 'E15', 'E16',
        'I0', 'I02', 'I03', 'I04', 'I05', 'I06',
        'O0', 'O02', 'O03', 'O04', 'O05', 'O06',
        'O1', 'O12', 'O13', 'O14', 'O15', 'O16',
        'O2', 'O22', 'O23', 'O24', 'O25', 'O26',
        'U0', 'U02', 'U03', 'U04', 'U05', 'U06',
        'U1', 'U12', 'U13', 'U14', 'U15', 'U16',
        'Y0', 'Y02', 'Y03', 'Y04', 'Y05', 'Y06',
        'd81', 'd91', 'D81', 'D91');

    $encoded = preg_replace($origin, $codes, $text);
    return $encoded;
}

function ViSort($list) { // $list must be an array
    for ($i = 0; $i < count($list); $i++) {
        $list_encoded[$i] = ViPAencode($list[$i]);
        $list_encoded[$i] = preg_replace("/\b(\w+)([012])([2-6])(\w+)?\b/", "$1$2$4$3", $list_encoded[$i]);
    }
    // Convert to lowercase
    $list_encoded_lowercase = array_map('strtolower', $list_encoded);
    // Sort list by encoded list (in lowercase),
    // so we don't have to write the crazy function called 'ViPAdecode' :p 
    array_multisort($list_encoded_lowercase, SORT_ASC, SORT_STRING, $list);

    return $list;
}

function ViSortByName($list) { // $list must be an array
// TODO: The priority must be checked again!
// There are some related articles on Journal of Language
// (printed about 5 years ago)...
    for ($i = 0; $i < count($list); $i++) {
        $list_reversed = explode(" ", trim($list[$i]));
        $list_reversed = array_reverse($list_reversed);
        $list[$i] = implode(" ", $list_reversed);
    }

    $list_sorted = ViSort($list);

    for ($j = 0; $j < count($list_sorted); $j++) {
        $list_unreversed = explode(" ", $list_sorted[$j]);
        $list_unreversed = array_reverse($list_unreversed);
        $list_sorted[$j] = implode(" ", $list_unreversed);
    }

    return $list_sorted;
}

$i = 0;
$employees = array();
while ($row = mysql_fetch_array($sqlempworking)) {
    $employees[$row[IDEmployee] . '-' . $row[FullName]][] = array($row[IDEmployee], $row[FullName], $row[Birthday], $row[UserName], $row[SexName], $row[DepartmentName], $row[Position], $row[StartDate], $row['CMND'], $row['Address'], $row['Mobile']);
    $mangemployees[] = $row[IDEmployee] . '-' . $row[FullName];
}
$mangemployees = ViSortByName($mangemployees);
$_SESSION['employeeworking']=$employees;
$_SESSION['mangemployeeworking']=$mangemployees;
$j=0;
for ($i = 0; $i < count($mangemployees); $i++) {
	$j++;
	if ( $j > $page_start && $j <= $page_end){
    ?>
    <tr>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $i + 1; ?></td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: left;" scope="col"><a href="employeemanager.php?content=admin/employeemanagement/content-edit-working.php&id=<?= $employees[$mangemployees[$i]][0][0] ?>" ><?php echo $employees[$mangemployees[$i]][0][1]; ?></a></td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][2]; ?></td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><a href="employeemanager.php?content=admin/employeemanagement/content-edit-working.php&id=<?= $employees[$mangemployees[$i]][0][0] ?>" ><?php echo $employees[$mangemployees[$i]][0][3] ?></a></td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][4] ?></td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][5] ?></td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][6] ?></td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][7] ?></td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][8] ?></td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][9] ?></td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][10] ?></td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col">
            <?php
            //$temp_end=strtotime("now");
            //$temp_start = explode(" ", $row[StartDate]);
            //$temp_start = strtotime($temp_start[0]);
            //echo (floor($temp_end / (60 * 60 * 24)) - floor($temp_start / (60 * 60 * 24)));
            $date1 = $employees[$mangemployees[$i]][0][7];
            $date2 = date("Y-m-d");

            $diff = abs(strtotime($date2) - strtotime($date1));

            $years = floor($diff / (365 * 60 * 60 * 24));
            $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
            $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

            printf("%d $l_year, %d $l_month, %d $l_date\n", $years, $months, $days);
            ?>
        </td>
        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col">
            <a href="#" onclick="if(confirm('<?php echo $l_confirm_deleteproject ?>')){  onclickDelete('<?= $employees[$mangemployees[$i]][0][0] ?>',this); return true;}else{return false;}"><?= $l_delete ?></a></td>
    </tr>
<?php }
}
?>

<tr class="trfoot">
    <td class="rf-dt-ftr-c" scope="colgroup" colspan="13">
        <div style="text-align: center; width: 100%">
            <span class="rf-ds">

                <?php
                if ($number_of_page > 1) {
                    if ($page == 1)
                        $p = 2;
                    else if ($page == $number_of_page)
                        $p = $number_of_page - 1;
                    else
                        $p=$page;
                    if ($page != 1) {
                        ?>
                        <a onclick="paging(1,'')" ><span class='button_next'><?php echo "<<<"; ?></span> </a>
                        <a onclick="paging(<?= ($page - 1) ?>,'')" ><span class='button_next'><?php echo "<<"; ?></span> </a>
                    <?php
                    } else {
                        echo "<span class='button_next'><<<</span>";
                        echo "<span class='button_next'><<</span>";
                    }
                    for ($i = $p - 1; $i <= $p + 1; $i++) {
                        if ($i <= $number_of_page && $i > 0)
                            if ($i == $page) {
                                echo "<span class='button_next' style='color: #000'><i>" . $i . "</i></span>";
                            } else {
                                ?>
                                <a onclick="paging(<?= $i ?>,'')" ><span class="button_next">
                                        <?php
                                        echo $i;
                                        ?>
                                    </span> </a>
                                <?php
                            }
                        ?>

                        <?php
                    }

                    if ($page != $number_of_page) {
                        ?>
                        <a onclick="paging(<?= ($page + 1) ?>,'')"><span class='button_next'><?php echo ">>"; ?></span> </a>
                        <a onclick="paging(<?= ($number_of_page) ?>,'')" ><span class='button_next'><?php echo ">>>"; ?></span> </a>
                    <?php
                    } else {
                        echo "<span class='button_next'>>></span> ";
                        echo "<span class='button_next'>>>></span> ";
                    }
                }
                ?>

            </span>
        </div>
    </td>
</tr>

<?php mysql_close($connection_ajax); ?>